package com.ce.thd;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.ce.po.NTree;
import com.ce.po.NTreeItem;
import com.ce.po.VChannelDO;
import com.ce.po.VTagDO;
import com.ce.po.VTypeDO;
import com.ce.utils.EtvNetWork;
import com.ce.utils.NWTransform;

public class InitTreeThread extends Thread {
	private List<VChannelDO> channels = null;
	private Tree tree;
	private NTree nTree;
	private Display display;
	
	public InitTreeThread(List<VChannelDO> channels, Tree tree, NTree nTree,Display display) {
		this.channels = channels;
		this.tree = tree;
		this.nTree = nTree;
		this.display = display;
	}

	@Override
	public void run() {
		try {
			for (final VChannelDO channelDO : channels) {
				Thread.sleep(500);
				if(!this.display.isDisposed()) {
					Runnable runnable = new Runnable() {
						public void run() {
							TreeItem channelItem = new TreeItem(tree,SWT.NONE);
							channelItem.setText(channelDO.getName());
							channelItem.setData(channelDO);
							NTreeItem nchannelItem = new NTreeItem(nTree);
							nchannelItem.setData(channelDO);
							String tagsJson = EtvNetWork.getTags(channelDO.getChannel());
							List<VTagDO> tags = NWTransform.transformJsonToTags(tagsJson);
							for (VTagDO tagDO : tags) {
								TreeItem tagItem = new TreeItem(channelItem,SWT.NONE);
								tagItem.setText(tagDO.getName());
								tagItem.setData(tagDO);
								NTreeItem ntagItem = new NTreeItem(nchannelItem);
								ntagItem.setData(tagDO);
								for (VTypeDO typeDO : tagDO.getTypes()) {
									TreeItem typeItem = new TreeItem(tagItem,SWT.NONE);
									typeItem.setText(typeDO.getName());
									typeItem.setData("type",typeDO);
									typeItem.setData(typeDO);
									typeItem.setData("channel",channelDO);
									NTreeItem ntypeItem = new NTreeItem(ntagItem);
									ntypeItem.setData("type",typeDO);
									ntypeItem.setData(typeDO);
									ntypeItem.setData("channel",channelDO);
								}
							}
							System.out.println("end");
						}
						
					};
					display.asyncExec(runnable); 
				}
				
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
